<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::Reflection::ClassMethods</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveRecord::Reflection::ClassMethods 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activerecord/lib/active_record/reflection_rb.html">activerecord/lib/active_record/reflection.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p><a href="../Reflection.html">Reflection</a> enables to interrogate Active
Record classes and objects about their associations and aggregations. This
information can, for example, be used in a form builder that takes an
Active Record object and creates input fields for all of the attributes
depending on their type and displays the associations to other objects.</p>

<p><a href="MacroReflection.html">MacroReflection</a> class has info for
AggregateReflection and AssociationReflection classes.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-create_reflection">create_reflection</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-reflect_on_aggregation">reflect_on_aggregation</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-reflect_on_all_aggregations">reflect_on_all_aggregations</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-reflect_on_all_associations">reflect_on_all_associations</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-reflect_on_all_autosave_associations">reflect_on_all_autosave_associations</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-reflect_on_association">reflect_on_association</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-create_reflection">
            
              <b>create_reflection</b>(macro, name, scope, options, active_record)
            
            <a href="ClassMethods.html#method-i-create_reflection" name="method-i-create_reflection" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-create_reflection_source')" id="l_method-i-create_reflection_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/reflection.rb#L20" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-create_reflection_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/reflection.rb, line 20</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">create_reflection</span>(<span class="ruby-identifier">macro</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">scope</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">active_record</span>)
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">macro</span>
  <span class="ruby-keyword">when</span> <span class="ruby-value">:has_many</span>, <span class="ruby-value">:belongs_to</span>, <span class="ruby-value">:has_one</span>, <span class="ruby-value">:has_and_belongs_to_many</span>
    <span class="ruby-identifier">klass</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:through</span>] <span class="ruby-operator">?</span> <span class="ruby-constant">ThroughReflection</span> <span class="ruby-operator">:</span> <span class="ruby-constant">AssociationReflection</span>
    <span class="ruby-identifier">reflection</span> = <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">macro</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">scope</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">active_record</span>)
  <span class="ruby-keyword">when</span> <span class="ruby-value">:composed_of</span>
    <span class="ruby-identifier">reflection</span> = <span class="ruby-constant">AggregateReflection</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">macro</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">scope</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">active_record</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">reflections</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">reflections</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">reflection</span>)
  <span class="ruby-identifier">reflection</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-reflect_on_aggregation">
            
              <b>reflect_on_aggregation</b>(aggregation)
            
            <a href="ClassMethods.html#method-i-reflect_on_aggregation" name="method-i-reflect_on_aggregation" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the AggregateReflection object for the named
<code>aggregation</code> (use the symbol).</p>

<pre class="ruby"><span class="ruby-constant">Account</span>.<span class="ruby-identifier">reflect_on_aggregation</span>(:<span class="ruby-identifier">balance</span>) <span class="ruby-comment"># =&gt; the balance AggregateReflection</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-reflect_on_aggregation_source')" id="l_method-i-reflect_on_aggregation_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/reflection.rb#L42" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-reflect_on_aggregation_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/reflection.rb, line 42</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">reflect_on_aggregation</span>(<span class="ruby-identifier">aggregation</span>)
  <span class="ruby-identifier">reflection</span> = <span class="ruby-identifier">reflections</span>[<span class="ruby-identifier">aggregation</span>]
  <span class="ruby-identifier">reflection</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">AggregateReflection</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-reflect_on_all_aggregations">
            
              <b>reflect_on_all_aggregations</b>()
            
            <a href="ClassMethods.html#method-i-reflect_on_all_aggregations" name="method-i-reflect_on_all_aggregations" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns an array of AggregateReflection objects for all the aggregations in
the class.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-reflect_on_all_aggregations_source')" id="l_method-i-reflect_on_all_aggregations_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/reflection.rb#L34" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-reflect_on_all_aggregations_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/reflection.rb, line 34</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">reflect_on_all_aggregations</span>
  <span class="ruby-identifier">reflections</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">grep</span>(<span class="ruby-constant">AggregateReflection</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-reflect_on_all_associations">
            
              <b>reflect_on_all_associations</b>(macro = nil)
            
            <a href="ClassMethods.html#method-i-reflect_on_all_associations" name="method-i-reflect_on_all_associations" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns an array of AssociationReflection objects for all the associations
in the class. If you only want to reflect on a certain association type,
pass in the symbol (<code>:has_many</code>, <code>:has_one</code>,
<code>:belongs_to</code>) as the first parameter.</p>

<p>Example:</p>

<pre>Account.reflect_on_all_associations             # returns an array of all associations
Account.reflect_on_all_associations(:has_many)  # returns an array of all has_many associations</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-reflect_on_all_associations_source')" id="l_method-i-reflect_on_all_associations_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/reflection.rb#L57" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-reflect_on_all_associations_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/reflection.rb, line 57</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">reflect_on_all_associations</span>(<span class="ruby-identifier">macro</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-identifier">association_reflections</span> = <span class="ruby-identifier">reflections</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">grep</span>(<span class="ruby-constant">AssociationReflection</span>)
  <span class="ruby-identifier">macro</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">association_reflections</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">reflection</span><span class="ruby-operator">|</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">macro</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">macro</span> } <span class="ruby-operator">:</span> <span class="ruby-identifier">association_reflections</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-reflect_on_all_autosave_associations">
            
              <b>reflect_on_all_autosave_associations</b>()
            
            <a href="ClassMethods.html#method-i-reflect_on_all_autosave_associations" name="method-i-reflect_on_all_autosave_associations" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns an array of AssociationReflection objects for all associations
which have <code>:autosave</code> enabled.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-reflect_on_all_autosave_associations_source')" id="l_method-i-reflect_on_all_autosave_associations_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/reflection.rb#L73" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-reflect_on_all_autosave_associations_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/reflection.rb, line 73</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">reflect_on_all_autosave_associations</span>
  <span class="ruby-identifier">reflections</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">reflection</span><span class="ruby-operator">|</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">options</span>[<span class="ruby-value">:autosave</span>] }
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-reflect_on_association">
            
              <b>reflect_on_association</b>(association)
            
            <a href="ClassMethods.html#method-i-reflect_on_association" name="method-i-reflect_on_association" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the AssociationReflection object for the <code>association</code>
(use the symbol).</p>

<pre>Account.reflect_on_association(:owner)             # returns the owner AssociationReflection
Invoice.reflect_on_association(:line_items).macro  # returns :has_many</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-reflect_on_association_source')" id="l_method-i-reflect_on_association_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/reflection.rb#L67" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-reflect_on_association_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/reflection.rb, line 67</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">reflect_on_association</span>(<span class="ruby-identifier">association</span>)
  <span class="ruby-identifier">reflection</span> = <span class="ruby-identifier">reflections</span>[<span class="ruby-identifier">association</span>]
  <span class="ruby-identifier">reflection</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">AssociationReflection</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    